//
//  WJMainGroupView.m
//  JerryIos
//
//  Created by 王军 on 2018/2/1.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJMainGroupView.h"
#import "UIImage+WJ.h"
#import "Masonry.h"

@interface WJMainGroupView()

@property (weak, nonatomic) UIImageView *arrowsView;  //arrows
@property (nonatomic, weak) UILabel *countView;  //数量
@property (nonatomic, weak) UIButton *titleView;  //标题

@end

@implementation WJMainGroupView

+(instancetype) groupViewWithTableView:(UITableView *) tableView {
    static NSString *cellId = @"groupCell";
    WJMainGroupView *groupView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellId];
    if (groupView == nil) {
        groupView = [[WJMainGroupView alloc] initWithReuseIdentifier:cellId];
    }
    return groupView;
}

//初始化方法，视图的frame和bounds没有值
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        //创建图片
        UIImageView *arrowView = [[UIImageView alloc] init];
        //设置图片为箭头
        arrowView.image = [UIImage imageNamed:@"icon_solid_arrows_right"];
        self.arrowsView = arrowView;
        
        //创建按钮
        UIButton *titleView = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置按钮背景
        [titleView setBackgroundImage:[UIImage resizableImage:@"group_bg_normal"] forState:UIControlStateNormal];
        [titleView setBackgroundImage:[UIImage resizableImage:@"group_bg_selected"] forState:UIControlStateHighlighted];
        //设置文字的颜色
        [titleView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置按钮内部文字的内边距
//        titleView.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        //设置按钮的内边距
        titleView.contentEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
        //设置按钮的内容左对齐
        titleView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //为按钮增加点击事件
        [titleView addTarget:self
                      action:@selector(titleViewClick)
            forControlEvents:UIControlEventTouchUpInside];
        self.titleView = titleView;

        //创建Label
        UILabel *countView = [[UILabel alloc] init];
        //设置label中文字为右对齐
        countView.textAlignment = NSTextAlignmentRight;
        //设置文字的颜色
        countView.textColor = [UIColor grayColor];
        self.countView = countView;

        [self.contentView addSubview:titleView];
        [self.contentView addSubview:arrowView];
        [self.contentView addSubview:countView];
    }
    return self;
}

//当一个控件的frame发生改变时就会调用，一般在这里布局子控件（设置子控件的frame）
-(void)layoutSubviews {
    [super layoutSubviews];
    
    //设置箭头的布局
    [self.arrowsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@20);
        make.height.equalTo(@20);
        make.left.equalTo(self.arrowsView.superview.mas_left).offset(10);
        make.centerY.equalTo(self.arrowsView.superview.mas_centerY);
    }];
    
    //设置按钮的布局
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.arrowsView.superview.mas_left);
        make.right.equalTo(self.arrowsView.superview.mas_right);
        make.top.equalTo(self.arrowsView.superview.mas_top);
        make.bottom.equalTo(self.arrowsView.superview.mas_bottom);
    }];
    
    //设置数量的布局
    CGFloat countH = self.frame.size.height;
    CGFloat countW = 150;
    [self.countView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(countW));
        make.height.equalTo(@(countH));
        make.right.equalTo(self.countView.superview.mas_right).offset(-10);
        make.centerY.equalTo(self.countView.superview.mas_centerY);
    }];
}

-(void)setGroup:(WJMainGroup *)group {
    _group = group;
    
    //设置按钮的文字
    [self.titleView setTitle:group.title forState:UIControlStateNormal];
    //设置数量
    self.countView.text = [NSString stringWithFormat:@"%lu",group.itemArray.count];
}

//当一个控件被添加到父控件中时被调用
-(void)didMoveToSuperview {
    //如果处于打开状态，箭头旋转90度，如果是关闭状态，箭头不旋转
    if (self.group.opened) {
        self.arrowsView.image = [UIImage imageNamed:@"icon_solid_arrows_down"];
    } else {
        self.arrowsView.image = [UIImage imageNamed:@"icon_solid_arrows_right"];
    }
}

//监听组名按钮的点击
-(void)titleViewClick {
    //修改打开或关闭状态
    self.group.opened = !self.group.isOpened;
    //刷新表格
    if ([self.delegate respondsToSelector:@selector(groupViewDidClick:)]) {
        [self.delegate groupViewDidClick:self];
    }
}

@end
